package com.example.test17_2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.text.format.Time;
import android.widget.Toast;

////继承Binder类绑定服务
//public class CurrentTimeService extends Service {
//    private final IBinder binder = new LocalBinder();
//    public class LocalBinder extends Binder {
//        CurrentTimeService getService(){
//            return CurrentTimeService.this;//返回当前服务的实例
//        }
//    }
//    @Override
//    public IBinder onBind(Intent arg0){
//        return binder;
//    }
//    public String getCurrentTime(){
//        Time time = new Time();//创建Time对象
//        time.setToNow();//设置时间为当前时间
//        String currentTime = time.format("%Y-%m-%d %H:%M:%S");//设置时间格式
//        return currentTime;
//    }
//}
//使用Messenger类绑定服务
public class CurrentTimeService extends Service {
    public static final int CURRENT_TIME = 0;

    private class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == CURRENT_TIME) {
                Time time = new Time();//创建Time对象
                time.setToNow();//设置时间为当前时间
                String currentTime = time.format("%Y-%m-%d %H:%M:%S");//设置时间格式
                Toast.makeText(CurrentTimeService.this,currentTime,Toast.LENGTH_LONG).show();
            }else{
                super.handleMessage(msg);
            }
        }
    }
    @Override
    public IBinder onBind(Intent intent){
        Messenger messenger = new Messenger(new IncomingHandler());
        return  messenger.getBinder();
    }
}
